
package com.xifan.myaccount;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.xifan.myaccount.entity.RecordDetail;
import com.xifan.myaccount.fragments.RecordEditor;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

import java.util.ArrayList;
import java.util.List;

public class AddRecord extends SwipeBackActivity {

    private List<Fragment> mFragmentList;
    private ViewPager mViewPager;
    private ActionBar mBar;
    private String mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_record);

        mBar = getActionBar();
        mBar.setDisplayHomeAsUpEnabled(true);

        int show;
        RecordDetail detail = getIntent().getParcelableExtra("detail");
        if (detail != null) {
            show = detail.getOperateType();
        } else {
            show = 0;
        }

        final String titleExpend = getString(R.string.add_record_title_expend);
        final String titleRevenue = getString(R.string.add_record_title_revenue);

        mTitle = show > 0 ? getString(R.string.add_record_title_edit)
                : getString(R.string.add_record_title);
        mBar.setTitle(mTitle + " " + titleExpend);

        Resources res = getResources();
        final ImageView mIndicator1st = (ImageView) findViewById(R.id.indicator_1);
        final ImageView mIndicator2nd = (ImageView) findViewById(R.id.indicator_2);
        final Drawable currentIndicator = res.getDrawable(R.drawable.ic_indicator_current);
        final Drawable defaultIndicator = res.getDrawable(R.drawable.ic_indicator_default);
        final LinearLayout indicatorLayout = (LinearLayout) findViewById(R.id.indicator);

        FragmentManager fm = getFragmentManager();
        mFragmentList = new ArrayList<Fragment>();

        int displayCounter = 0;
        if (show < 2) {
            RecordEditor expend = new RecordEditor();
            Bundle b = new Bundle();
            b.putInt("opType", 1);
            b.putParcelable("detail", detail);
            expend.setArguments(b);
            mFragmentList.add(expend);

            displayCounter++;
        }
        if (show != 1) {
            RecordEditor rev = new RecordEditor();
            Bundle b1 = new Bundle();
            b1.putInt("opType", 2);
            b1.putParcelable("detail", detail);
            rev.setArguments(b1);
            mFragmentList.add(rev);
            displayCounter++;
        }
        indicatorLayout.setVisibility(displayCounter > 1 ? View.VISIBLE : View.GONE);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new OperateTypeAdapter(fm));
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            public void onPageSelected(int index) {
                switch (index) {
                    case 0:
                        mBar.setTitle(mTitle + " " + titleExpend);
                        mIndicator1st.setImageDrawable(currentIndicator);
                        mIndicator2nd.setImageDrawable(defaultIndicator);
                        break;
                    case 1:
                        mBar.setTitle(mTitle + " " + titleRevenue);
                        mIndicator1st.setImageDrawable(defaultIndicator);
                        mIndicator2nd.setImageDrawable(currentIndicator);
                        break;
                }
            }

            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    private class OperateTypeAdapter extends FragmentPagerAdapter {
        public OperateTypeAdapter(FragmentManager fm) {
            super(fm);
        }

        public Fragment getItem(int location) {
            return mFragmentList.get(location);
        }

        public int getCount() {
            return mFragmentList.size();
        }

    }
}
